﻿using ResourceSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.IO;

public class VersionWindow  : EditorWindow
{

    static string targetPath;

    private List<string> resPaths=new List<string>();

    private bool isSelecteChanged = false;

    static string  path;

    int selectedToolBarId;

    static string curVersion = "";

    void OnSelectionChange()
    {
        isSelecteChanged = true;
    }

    [MenuItem("CustomTools/Resource/AssetBundle/VersionWindow")]
    static void OpenVersionWindow()
    {

        EditorWindow.GetWindow<VersionWindow>();
        LitJson.JsonData data = ResourceSystemFacade.Inst.ReadJson(ResourceSystemFacade.ConfigPath + "/GameSetting.json");
        curVersion = data["Version"].AsString;
    }

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        GUILayout.Space(10);
        GUILayout.Space(15);
        if (isSelecteChanged)
        {
            isSelecteChanged = false;
            resPaths.Clear();
        }

        switch (selectedToolBarId = GUILayout.Toolbar(selectedToolBarId, new string[] { "Debug","Release"}))
        {
            case 0 :
                path = ResourceSystemFacade.AbOutPath + "/Debug/";
                break;
            case 1:
                path = ResourceSystemFacade.AbOutPath + "/Release/";
                break;
        }
        GUILayout.Space(15);
        if (GUILayout.Button("资源数据整理"))
        {
            ResUtils.InitResInfo(Application.dataPath + "/Resources",  true);
        }
        GUILayout.Space(15);
        GUILayout.BeginHorizontal();
        GUILayout.Label("分类打包：");
        GUILayout.BeginVertical();
        if (GUILayout.Button("配置打包"))
        {
            ResourceSystemFacade.Inst.InitSystem(false);
            ABUtils.BuildAbAboutDependency(path + ResourceSystemFacade.Version+"/Config", new string[] { ResTypeFilter.ScriptableObject });
        }
        if (GUILayout.Button("预制体打包"))
        {
            ResourceSystemFacade.Inst.InitSystem(false);
            ABUtils.BuildAbAboutDependency(path+ ResourceSystemFacade.Version+"/Prefabs",new string[] { ResTypeFilter.Materials,ResTypeFilter.Prefabs});

        }
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUILayout.Space(15);

        GUILayout.Label("当前版本：  "  +curVersion);
        if (GUILayout.Button("发布"))
        {
            WriteDiffirencyData(path);
            ResourceSystemFacade.Inst.UpdateVersionToJson();
            curVersion = ResourceSystemFacade.Version;
        }


        GUILayout.EndVertical();
    }

    void WriteDiffirencyData(string path)
    {
        ResUtils.InitResInfo(Application.dataPath + "/Resources", false);
        List<ResourcesData> infos = ResourceSystemFacade.Inst.ResDict.Values.ToList();
        Dictionary<string, ResourcesData> previousInfo = ResUtils.GetPreviousVersionResData();
        List<ResourcesData> upgradeInfos = new List<ResourcesData>();
        for (int i = 0; i < infos.Count; i++)
        {
            if (previousInfo.ContainsKey(infos[i].Name))
            {
                if (previousInfo[infos[i].Name].ID != infos[i].ID)
                {
                    upgradeInfos.Add(infos[i]);
                    DebugUtils.LogRedInfo(infos[i].Name);
                }
            }
            else
            {
                upgradeInfos.Add(infos[i]);
                DebugUtils.LogRedInfo(infos[i].Name);
            }
        }

        List<string> deleteFiles = new List<string>();
        foreach (var item in previousInfo)
        {
            for (int i = 0; i < infos.Count; i++)
            {
                if (infos[i].Name == item.Key)
                {
                    break;
                }

                if (i == infos.Count - 1)
                {
                    deleteFiles.Add(item.Key);
                }
            }
        }

        string filePath = path + "/UpdateResInfo.json";

        if (!File.Exists(filePath))
        {
            StreamWriter stream =File.CreateText(filePath);
            stream.Write("{}");
            stream.Close();
        }
        LitJson.JsonData data = ResourceSystemFacade.Inst.ReadJson(filePath);
        LitJson.JsonData tempData = LitJson.JsonMapper.ToObject(LitJson.JsonMapper.ToJson(upgradeInfos)); 

        data[ResourceSystemFacade.Version.ToString()] = tempData;
        ResourceSystemFacade.Inst.WriteJson(filePath, data);
        DebugUtils.LogRedInfo(infos.Count.ToString());
        ConfigUtils.CreateScriptableObject<ResourcesInfo>(ResourceSystemFacade.Version, ConvertUtils.ConvertImplementListToInterfaceList(infos), ResourceSystemFacade.ResInfoConfigPath);
        FileInfo info = new FileInfo(ResourceSystemFacade.ResInfoConfigPath + ResourceSystemFacade.Version + ".asset");
        DebugUtils.LogRedInfo(info.FullName+info.Exists);
    }

    public static void ShowBar(string content,float progress)
    {
        EditorUtility.DisplayProgressBar("Show Progress", content, progress);

    }

}